package net.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "HISTOGRAMA")
public class Histogram implements Serializable {

	private static final long serialVersionUID = -1478247711536130877L;

	public static final String HISTOGRAMA_DELIMITER = ";";
	public static final String HISTOGRAMA_H = "H";
	public static final String HISTOGRAMA_S = "S";
	public static final String HISTOGRAMA_V = "V";
	public static final String HISTOGRAMA_GRIS = "GRIS";
	public static final String HISTOGRAMA_R = "R";
	public static final String HISTOGRAMA_G = "G";
	public static final String HISTOGRAMA_B = "B";
	public static final int MAX_VAL_HISTOGRAMA_H = 360;
	public static final int MAX_VAL_HISTOGRAMA_S = 100;
	public static final int MAX_VAL_HISTOGRAMA_V = 100;
	public static final int MAX_VAL_HISTOGRAMA_GRIS = 255;
	public static final int MAX_VAL_HISTOGRAMA_R = 255;
	public static final int MAX_VAL_HISTOGRAMA_G = 255;
	public static final int MAX_VAL_HISTOGRAMA_B = 255;

	@Id
	@Column(name = "uid")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	/**
	 * Tipo de histograma: R, G, B, H, S, V, etc.
	 */
	@Column(name = "tipo", nullable = false)
	private String tipo;

	/**
	 * Cantidad maxima de valores que se pueden cargar para un tipo de
	 * histograma dado. Ej: para los tipos R, G y B el numero maximo de valores
	 * sera 128, 256, etc.
	 */
	@Column(name = "max_valores", nullable = false)
	private Integer maxValores;

	/**
	 * El arreglo de valores del histograma separados por coma Agruegue esto:
	 * 
	 */
	@Column(name = "valores", nullable = false)
	private String valoresString = null;

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "histograms")
	private Set<Clase> clases = new HashSet<Clase>();// List<Clase> clases = new
														// ArrayList<Clase>();

	public Histogram() {
		super();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTipo() {
		return tipo;
	}

	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public Integer getMaxValores() {
		return maxValores;
	}

	public void setMaxValores(Integer maxValores) {
		this.maxValores = maxValores;
	}

	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof Histogram))
			return false;
		Histogram r = (Histogram) obj;

		return this.getTipo().equals(r.getTipo());
	}

	public String toString() {
		return this.getTipo();
	}

	public double[] getValores() {
		if (this.valoresString != null) {
			String values[] = this.valoresString.split(HISTOGRAMA_DELIMITER);
			double valores[] = new double[values.length];
			for (int i = 0; i < values.length; i++) {
				valores[i] = Double.valueOf(values[i]);
			}
			return valores;
		}
		return null;
	}

	public void setValores(double[] values) {
		actualizarValoresString(values);
	}

	public void actualizarValoresString(double[] valores) {
		if (valores != null) {
			StringBuffer buffer = new StringBuffer();
			for (double val : valores) {
				if (buffer.length() > 0) {
					buffer.append(HISTOGRAMA_DELIMITER);
				}
				buffer.append(val);
			}
			this.valoresString = buffer.toString();
			this.maxValores = getValores().length;
		} else {
			this.valoresString = null;
		}
	}

	public String getValoresString() {
		return valoresString;
	}

	public void setValoresString(String valuesString) {
		this.valoresString = valuesString;
	}

}
